# Copyright (C) 2009 Israel Herraiz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors : Israel Herraiz <isra@herraiz.org>

"""
@author:       Israel Herraiz
@license:      GNU GPL version 3 or any later version
@contact:      isra@herraiz.org
"""

import sys
import getopt
from version import x360_version
from main import Application

author = "(c) 2009 Israel Herraiz <isra@herraiz.org>"
name = "x360bright %s - Utility to set the LCD brightness in Samsung X360 laptops" % x360_version
credits = "\n%s\n%s\n" % (name, author)

def usage():
    print credits
    print "Usage: %s [options] COMMAND [BRIGHTNESS]" % sys.argv[0]
    print """
    where COMMAND is either 'inc', 'dec', 'get' or 'set' (without
    quotes), to increase, decrease, get or set the brightness,
    respectively. BRIGHTNESS is a  number between 0 (minimum
    brightness) and 100 (maximum brightness), and it has to be
    indicated only with the 'inc', 'dec' or 'set' commands.

Options:

    -h, --help        Show this message and exit.
    -v, --version     Show the version number and exit.
"""

def version():
    print x360_version
    
def start():

    short_opts = "hv"
    long_opts = ["help","version"]

    try:
        opts, args = getopt.getopt(sys.argv[1:], short_opts, long_opts)
    except getopt.GetoptError, (msg, opt):
        print >> sys.stderr, msg
        usage()
        sys.exit(2)

    if not args:
        usage()
        sys.exit(0)

    for opt, value in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif opt in ("-v", "--version"):
            version()
            sys.exit(0)


    try:
        command = args[0]
        if command != 'get':
            brightness = args[1]
        else:
            brightness = -1
    except IndexError:
        usage()
        sys.exit(2)
    
    myapp = Application(command, brightness)
    myapp.run()
    
